package com.lagou.edu.transaction;

import com.lagou.edu.annotation.Component;
import com.lagou.edu.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Kirito
 * @description:
 * @date 2020/04/04
 */
@Component
public class ConnectionUtils {

    private ThreadLocal<Connection> local = new ThreadLocal<>();

    /*private static ConnectionUtils connectionUtils = new ConnectionUtils();

    private ConnectionUtils() {
    }

    public static ConnectionUtils getInstance() {
        return connectionUtils;
    }*/

    public Connection getCurrentThreadConnection() throws SQLException {
        Connection connection = local.get();
        if (null == connection) {
            connection = DruidUtils.getInstance().getConnection();
            local.set(connection);
        }
        return connection;
    }
}
